package com.example.myjava;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by 用户 on 2016/11/22.
 */

public class Nameactivity extends Activity{
    TextView TouchView,histroy,eventlable;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.name);
        TouchView = (TextView) findViewById(R.id.touch_area);
        histroy = (TextView) findViewById(R.id.history_label);
        eventlable = (TextView) findViewById(R.id.event_label);

        TouchView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                switch (action) {
                    // 当按下的时候
                    case (MotionEvent.ACTION_DOWN):
                     //   Display("ACTION_DOWN", event);
                        break;
                    // 当按上的时候
                    case (MotionEvent.ACTION_UP):
                        int historysize = ProcessHistory(event);
                        histroy.setText("历史数据" + historysize);
                        Display("ACTION_UP", event);

                        break;
                    // 当触摸的时候
                    case (MotionEvent.ACTION_MOVE):
                      //  Display("ACTION_MOVE", event);
                }
                return true;
            }
        });
    }

    public void Display(String eventType, MotionEvent event) {
        // 触点相对坐标的信息
        int x = (int) event.getX();
        int y = (int) event.getY();
        // 表示触屏压力大小
        float pressure = event.getPressure();
        // 表示触点尺寸
        float size = event.getSize();
        // 获取绝对坐标信息
        int RawX = (int) event.getRawX();
        int RawY = (int) event.getRawY();
        String msg = "";

        msg += "事件类型" + eventType + "\n";
        msg += "相对坐标" + String.valueOf(x) + "," + String.valueOf(y) + "\n";
        msg += "绝对坐标" + String.valueOf(RawX) + "," + String.valueOf(RawY)
                + "\n";
        msg += "触点压力" + String.valueOf(pressure) + ",";
        msg += "触点尺寸" + String.valueOf(size) + "\n";
        eventlable.setText(msg);
//        TouchView.add
    }

    public int ProcessHistory(MotionEvent event) {
        int history = event.getHistorySize();
        for (int i = 0; i < history; i++) {
            long time = event.getHistoricalEventTime(i);
            float pressure = event.getHistoricalPressure(i);
            float x = event.getHistoricalX(i);
            float y = event.getHistoricalY(i);
            float size = event.getHistoricalSize(i);
        }

        return history;

    }

}